//
//  WorkRegistrationViewController.m
//  Runone
//
//  Created by runone01 on 16/9/7.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "WorkRegistrationViewController.h"
#import "StationTableViewController.h"
#import "StationModel.h"
#import "RequestHelper.h"
#import "SysDutyRecordInfoModel.h"
#import "ParModel.h"
#import "MJExtension.h"
#import "SysPersonalSettingInfoModel.h"
#import "CommonUtilityjudge.h"
#import "PromptUtil.h"
#import "SVProgressHUD.h"
#import "AppDelegate.h"

@interface WorkRegistrationViewController ()<UIPopoverPresentationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *dropBtn;
@property (weak, nonatomic) IBOutlet UITextField *StationChooseTF;
@property(nonatomic,strong)StationTableViewController *station;
@property (weak, nonatomic) IBOutlet UIButton *UpWork;
@property (weak, nonatomic) IBOutlet UIButton *DownWork;
@property(nonatomic,strong)SysDutyRecordInfoModel *dutyModel;
@property(nonatomic,strong)ParModel *parModel;
@property(nonatomic,strong)SysPersonalSettingInfoModel *Regis;
@property (weak, nonatomic) IBOutlet UILabel *Name;
@property (weak, nonatomic) IBOutlet UIButton *cancle;
@property(nonatomic,strong)PromptUtil *Prompt;
@property (weak, nonatomic) IBOutlet UIButton *confirm;
@property (weak, nonatomic) IBOutlet UIButton *workUpImageBtn;
@property (weak, nonatomic) IBOutlet UIButton *workDownImageBtn;
@property(nonatomic,strong)NSArray *AllStationNameArray;
@end

@implementation WorkRegistrationViewController

#pragma    -------值班登记



-(void)viewWillAppear:(BOOL)animated
{
    _StationChooseTF.text=@"";
    _Name.text=@"";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    _dropBtn.backgroundColor = [UIColor yellowColor ];
    _StationChooseTF.enabled = NO;
    AppDelegate *myDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
    NSArray *arr=myDelegate.AllTollStationArray;
    _AllStationNameArray=[arr valueForKey:@"TollStationName"];
    _Prompt=[[PromptUtil alloc] init];
    [_workUpImageBtn setBackgroundImage:[UIImage imageNamed:@"endwork"] forState:UIControlStateNormal];
    [_workDownImageBtn setBackgroundImage:[UIImage imageNamed:@"endwork"] forState:UIControlStateNormal];
    __weak typeof(self) weakSelf =self;
    _dutyModel=[[SysDutyRecordInfoModel alloc] init];
    _parModel=[[ParModel alloc] init];
    _station=[[StationTableViewController alloc] init];
    _station.NameBlock=^(NSString *str){
        weakSelf.Name.text=str;
    };
    
    _station.modalPresentationStyle = UIModalPresentationPopover;
    self.preferredContentSize       = CGSizeMake(320, 260);
    
//    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(100, 200, 26, 28)];
   // btn.backgroundColor=[UIColor clearColor];
    _StationChooseTF.layer.borderColor = [UIColor colorWithRed:235.0 / 255 green:235.0 / 255 blue:235.0 / 255 alpha:1].CGColor;
    _StationChooseTF.layer.borderWidth = 1.0;
    _station.LineBlock=^(StationModel *model){
        weakSelf.StationChooseTF.text=[NSString stringWithFormat:@"%@",model.TollStationName];
        weakSelf.dutyModel.TollStationUID=model.TollStationUID;
    };
//    _StationChooseTF.rightView=btn;
//    [btn addTarget:self action:@selector(drop:) forControlEvents:UIControlEventTouchUpInside];
//    [btn setBackgroundImage:[UIImage imageNamed:@"xiala"] forState:UIControlStateNormal];
//    _StationChooseTF.rightViewMode=UITextFieldViewModeAlways;
//    NSLog(@"%@",_AllStationNameArray);
    // Do any additional setup after loading the view from its nib.
}

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

//收费站点击
- (IBAction)drop:(id)sender {
    NSLog(@"dropBtn");
    _station.popoverPresentationController.sourceView=_dropBtn    ;
    _station.popoverPresentationController.delegate=self;
    _station.popoverPresentationController.sourceRect=CGRectMake(110, 0, -80, 255);
    _station.popoverPresentationController.permittedArrowDirections=NO;
    [self presentViewController:_station animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//上班登记
- (IBAction)UPWorkBtn:(UIButton *)sender {

    [self upWork];
}

//下班登记
- (IBAction)DownWorkBtn:(UIButton *)sender {
    
    [self downWork];

}
//取消按钮
- (IBAction)Cancle:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

//确定按钮
- (IBAction)confirm:(UIButton *)sender {

    if (stringIsEmpty(_StationChooseTF.text)) {
        [_Prompt alert:@"请选择收费站！"];
        return;
    }
    if (!_dutyModel.DutyType) {
        [_Prompt alert:@"请选择上下班登记"];
        return;
    }
    if (![_AllStationNameArray containsObject:_StationChooseTF.text]) {
        [_Prompt alert:@"您输入的收费站不存在"];
        return;
    }
    [self request];
}

//刷新数据
-(void)request
{
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCurrentDutyPersonalName;
    getCurrentEventModel.DataValue = [self jsonstring];
    getCurrentEventModel.UpdatedTime=[requestHelper getCurrentTime];
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"SaveSysDutyRecordInfo";
    
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            [SVProgressHUD showSuccessWithStatus:@"登记成功"];
            [self dismissViewControllerAnimated:YES completion:nil];

        }
    };

    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}

-(NSString *)jsonstring
{
    
    NSMutableDictionary *dic=_dutyModel.mj_keyValues;
    NSString *temp=[dic mj_JSONString];
    _parModel.key=@"SysDutyRecordInfo";
    _parModel.value=temp;
    
    NSMutableDictionary *dicc=_parModel.mj_keyValues;
    NSString *dateValue=[dicc mj_JSONString];
    NSString *str=@"[";
    NSString *tempp=[str stringByAppendingString:dateValue];
    NSString *str1=@"]";
    NSString *JsonString=[tempp stringByAppendingString:str1];
    return JsonString;
}
//上班登记
- (IBAction)changeUpImage:(UIButton *)sender {
    [self upWork];
}
//下班登记
- (IBAction)changeDownImage:(UIButton *)sender {
    [self downWork];
}

//上班登记
-(void)upWork {
    _dutyModel.DutyType=@"1";
    [_workUpImageBtn setBackgroundImage:[UIImage imageNamed:@"startwork"] forState:UIControlStateNormal];
    [_workDownImageBtn setBackgroundImage:[UIImage imageNamed:@"endwork"] forState:UIControlStateNormal];
}

//下班登记
-(void)downWork {
    _dutyModel.DutyType=@"2";
    [_workUpImageBtn setBackgroundImage:[UIImage imageNamed:@"endwork"] forState:UIControlStateNormal];
    [_workDownImageBtn setBackgroundImage:[UIImage imageNamed:@"startwork"] forState:UIControlStateNormal];
}


@end
